package DP;

/**
 * @author 曹见朋
 * @create 2024-06-12-22:01
 */
public class Main1049 {
    // TODO 1049. 最后一块石头的重量 II
    public static int lastStoneWeightII(int[] stones) {
        int sum = 0;
        for(int stone : stones){
            sum += stone;
        }
        int half1 = sum / 2;
        int[] dp = new int[half1 + 1];

        dp[0] = 0;

        for(int i = 0; i < stones.length; i++){
            for(int j = half1; j >= stones[i]; j--){
                dp[j] = Math.max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        return sum - 2 * dp[half1];
    }

    public static void main(String[] args) {
        int[] stones = new int[]{2,7,4,1,8,1};
        int i = lastStoneWeightII(stones);
        System.out.println(i);
    }
}
